import re
import gtk

class Validator:
    def __init__(self,locations=None):
        self.validators = []
        self.locations = locations
        self.REGEX = {"REGEX_IP" : "^([01]?\d\d?|2[0-4]\d|25[0-5])\.([01]?\d\d?|2[0-4]\d|25[0-5])\.([01]?\d\d?|2[0-4]\d|25[0-5])\.([01]?\d\d?|2[0-4]\d|25[0-5])$",
             "REGEX_DOMAIN" : "^([a-zA-Z0-9_\-]+(\.[a-zA-Z0-9_\-]+)*)?$",
             "REGEX_MOUNT_POINT" : "^/[a-zA-Z0-9_\-\.]+(/[a-zA-Z0-9_\-\.]+)*$",
             "REGEX_PATH" : "^//(([01]?\d\d?|2[0-4]\d|25[0-5])\.([01]?\d\d?|2[0-4]\d|25[0-5])\.([01]?\d\d?|2[0-4]\d|25[0-5])\.([01]?\d\d?|2[0-4]\d|25[0-5])/[a-zA-Z0-9_\-\.]+(/[a-zA-Z0-9_\-\.]+)*|[a-zA-Z0-9_\-]+(\.[a-zA-Z0-9_\-]+)*/[a-zA-Z0-9_\-\.]+(/[a-zA-Z0-9_\-\.]+)*)$",
             "REGEX_LOGIN" : "^\S+$",
             "REGEX_PASSWORD" : "^\S+$",
             "REGEX_NAME" : "^.+$"}
    
    def add(self,TB,REGEX,Lbl=None):
        self.validators.append((TB,REGEX,Lbl))
            
    def isvalid(self):
        isvalid = True
        for validator in self.validators:
            if validator[1] in self.REGEX:
                REGEX = self.REGEX[validator[1]]
            else:
                REGEX = validator[1]
                
            if re.match(REGEX,validator[0].get_text()):
                #print validator[0].get_text()+" match ",validator[1]
                if validator[2] != None:
                    validator[2].modify_fg(gtk.STATE_NORMAL,gtk.gdk.Color(0,0,0,0))
            else:
                #print validator[0].get_text()+" dosen't match ",validator[1]
                isvalid = False
                if validator[2] != None:
                    validator[2].modify_fg(gtk.STATE_NORMAL,gtk.gdk.Color(65535,0,0,0))
        return isvalid
        
if __name__ == "__main__":
    import pygtk
    pygtk.require("2.0")
    import gtk, gobject
    from location import Location
    from share import Share
    
    class Test_Window():
        def __init__(self,location=None):
            """constructor"""
            self.validator = Validator()
            
            self.window = gtk.Window(gtk.WINDOW_TOPLEVEL)
            #some signals handelers
            self.window.connect("destroy", self.destroy)
            self.window.connect("delete_event", self.delete_event)
            
            self.Lbl_expr = gtk.Label("Expression :")
            self.Lbl_regex = gtk.Label("Regex:")
            self.TB_expr = gtk.Entry(max=0)
            self.TB_regex = gtk.Entry(max=0)
            #submit button
            self.Btn_Sumbit = gtk.Button("Submit")
            self.Btn_Sumbit.connect("clicked", self.submit, None)
            
            #Boxes
            self.expr_HBox = gtk.HBox(False, 5)
            self.regex_HBox = gtk.HBox(False, 5)
            self.Submit_HBox = gtk.HBox(False, 5)
            self.Global_VBox = gtk.VBox(False, 0)
            
            #Packing
            self.expr_HBox.pack_start(self.Lbl_expr, False, False, 10)
            self.expr_HBox.pack_start(self.TB_expr, False, False, 10)
            self.regex_HBox.pack_start(self.Lbl_regex, False, False, 10)
            self.regex_HBox.pack_start(self.TB_regex, False, False, 10)
            self.Submit_HBox.pack_start(self.Btn_Sumbit, False, False, 10)
            self.Global_VBox.pack_start(self.expr_HBox, False, False, 10)
            self.Global_VBox.pack_start(self.regex_HBox,False,False,10)
            self.Global_VBox.pack_start(self.Submit_HBox, False, False, 10)
            
            self.window.add(self.Global_VBox)
            #Show
            self.window.show_all() 
        
        def Is_Valid(self):
            self.validator.validators = []
            self.validator.add(self.TB_expr,self.TB_regex.get_text())
            if self.validator.isvalid():
                return True
            return False
        
        def submit(self, widget, data=None):
            self.Is_Valid()

            
        def delete_event(self, widget, event):
           """delete_event is call when the close button is pressed"""
           return False #False call the destroy signal True don't          
       
        def destroy(self,widget):
           gtk.quit()
           
        def main(self):
           gtk.main()
           
    window = Test_Window()
    window.main()  